package com.example.testone.uiComponents;


import static com.example.testone.uiComponents.WordsRadioAttrs.STYLE_BORDER;
import static com.example.testone.uiComponents.WordsRadioAttrs.STYLE_NORMAL;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.example.testone.R;


/**
 * @Author: dreamgyf
 * @Data: 2021/6/21
 */
public class WordsRadioItem extends AppCompatTextView {

	private final static int DEFAULT_STYLE = STYLE_NORMAL;

	@WordsRadioAttrs.Style
	private int mStyle = DEFAULT_STYLE;

	private boolean mIsSelected;

	private boolean mDisable;

	@ColorInt
	private int mUnselectedColor;

	@ColorInt
	private int mSelectedColor;

	public WordsRadioItem(@NonNull Context context) {
		super(context);
		init(null);
	}

	public WordsRadioItem(@NonNull Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}

	public WordsRadioItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(attrs);
	}

	private void init(AttributeSet attrs) {
		setCompoundDrawablePadding(getContext().getResources().getDimensionPixelSize(R.dimen.padding2));
		setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimensionPixelSize(R.dimen.textsize14));
		if (attrs != null) {
			TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WordsRadioItem);
			mIsSelected = typedArray.getBoolean(R.styleable.WordsRadioItem_selected, false);
			mDisable = typedArray.getBoolean(R.styleable.WordsRadioItem_disable, false);
			mUnselectedColor = typedArray.getColor(R.styleable.WordsRadioItem_unselectedTextColor, ContextCompat.getColor(getContext(), R.color.biz_wg_color_333333));
			mSelectedColor = typedArray.getColor(R.styleable.WordsRadioItem_selectedTextColor, ContextCompat.getColor(getContext(), R.color.biz_wg_color_333333));
			typedArray.recycle();
		} else {
			mUnselectedColor = ContextCompat.getColor(getContext(), R.color.biz_wg_color_333333);
			mSelectedColor = ContextCompat.getColor(getContext(), R.color.biz_wg_color_333333);
		}
		select(mIsSelected);
	}

	public void setStyle(@WordsRadioAttrs.Style int style) {
		mStyle = style;
		if (style == STYLE_NORMAL) {
			setPadding(0, 0, 0, 0);
			setBackground(null);
		} else if (style == STYLE_BORDER) {
			int horizontalPadding = getContext().getResources().getDimensionPixelSize(R.dimen.padding8);
			int verticalPadding = getContext().getResources().getDimensionPixelSize(R.dimen.padding3);
			setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
			setBackground(new BorderLayout.BorderDrawableBuilder()
					.color(ContextCompat.getColor(getContext(), R.color.biz_wg_color_28bea0))
					.width(ScreenUtil.dp2px(1))
					.radius(getContext().getResources().getDimension(R.dimen.width2))
					.build());
		}
		select(mIsSelected);
	}

	public void select(boolean select) {
		mIsSelected = select;
//		setCompoundDrawablesWithIntrinsicBounds(select ? R.drawable.biz_wg_icon_radio_selected : R.drawable.biz_wg_icon_radio_not_selected,
//				0, 0, 0);
		if (mStyle == STYLE_BORDER) {
			setBackground(new BorderLayout.BorderDrawableBuilder()
					.color(ContextCompat.getColor(getContext(), select ? R.color.biz_wg_color_28bea0 : R.color.biz_wg_color_cccccc))
					.width(ScreenUtil.dp2px(1))
					.radius(getContext().getResources().getDimension(R.dimen.width2))
					.build());
		}
		setTextColor(select ? mSelectedColor : mUnselectedColor);

		if (select && mInnerOnSelectListener != null) {
			mInnerOnSelectListener.onSelect(this);
		}
	}

	public boolean isSelected() {
		return mIsSelected;
	}

	public void disable(boolean disable) {
		mDisable = disable;
		setEnabled(!disable);
		setAlpha(disable ? 0.5f : 1f);
	}

	public boolean isDisable() {
		return mDisable;
	}

	InnerOnSelectListener mInnerOnSelectListener;

	interface InnerOnSelectListener {
		void onSelect(View v);
	}

	@Override
	public void setOnClickListener(@Nullable final OnClickListener l) {
		super.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				select(true);
				if (l != null) {
					l.onClick(v);
				}
			}
		});
	}
}
